<?php
drupal_add_js('js/jquery.validate.min.js');
drupal_add_js('js/raovat.form.js');
drupal_add_js('js/loadsubcate.js');
drupal_add_js('js/jquery-ui-1.8.16.custom.min.js');
drupal_add_css('css/ui-lightness/jquery-ui-1.8.16.custom.css');
drupal_add_css('css/style.dev1.css');
drupal_add_css('css/f_form.css');

$arrayLocate    = getAllLocationInfo();
$arrayType      = array(1=>"Hàng mới", 2=>"Hàng cũ", 3=>"Hàng xách tay");
$arrayForm      = array(1=>"Cần bán", 2=>"Cần mua", 3=>"Cho thuê", 4=>"Cần thuê");
$arrayTitle     = array(1=>"Nơi bán", 2=>"Nơi cần mua", 3=>"Nơi cho thuê", 4=>"Nơi cần thuê");

/** Get technical info */
$action     = 'post';
$cate_id    = 0;
if($action  == 'post') {
    $cate_id    = $data["cate_id"];
}
if(isset($data["rid"])) $action = 'edit';
if($action  == 'edit') {
    $rinfo      = getRecordById('raovat', 'rid', $data["rid"]);
    $cate_id    = $rinfo[$data["rid"]]["cate_id"];
}

$data["att"]    = getAttributes($cate_id);
//drupal_debugItem($data["att"]);
?>
<form id="fRaovat" name="post_raovat" action="<?php print url('admin/raovat/submit/' . $data["rid"]) ?>" method="POST" enctype="multipart/form-data">
    <!-- Bloch info base -->
    <div id="post_form">
        <div id="error"><ul></ul></div>
        <input type="hidden" name="action" value="<?php print $action?>" />
        <input type="hidden" name="cate_id" value="<?php print $cate_id?>" />
        <table class="post_raovat" cellpadding="3" cellspacing="0" border="0" width="99%">
            <tr>
                <td class="right bold">Danh mục rao vặt : </td>
                <td>
                    <?php
                    $rowCate = getRecordById('category','cate_id',$cate_id);
                    ?>
                    <a onclick="$('#showcate').css('display', 'block')" href="#" id="cate_name"><?php print $rowCate[$cate_id]["cate_name"]?></a>
                </td>
            </tr>
            <tr>
                <td class="right bold"><span>Tiêu đề rao vặt : </span></td>
                <td>
                    <input class="txt" type="text" name="title" class="title" value="<?php print $rinfo[$data["rid"]]["title"]?>" />
                </td>
            </tr>
            <tr>
                <td class="right bold"><span>Ảnh đại diện : </span></td>
                <td>
                    <div>
                        <img width="150px" height="100px" src="<?php print base_path() . 'files/raovat/' . $rinfo[$data["rid"]]["image"] ?>" />
                    </div>
                    <input size="62" type="file" name="image" class="image" />
                    <span> ( .jpg .gif .png .jpeg )</span>
                </td>
            </tr>
            <tr>
                <td class="right bold"><span>Loại hàng : </span></td>
                <td>
                    <?php
                    foreach($arrayType as $key=>$type) {
                        $checked = "";
                        if($rinfo[$data["rid"]]["type"] == $key || $key == 1) {
                            $checked = "checked='checked'";
                        }
                        ?>    
                        <input <?php print $checked?> name="type" type="radio" value="<?php print $key?>"/><?php print $type?>
                        <?php 
                    }
                    ?>
                </td>
            </tr>
            <tr>
                <td class="right bold"><span>Giá : </span></td>
                <td>
                    <input type="text" name="price" value="<?php print $rinfo[$data["rid"]]["price"]?>" onkeyup="changePriceText('show_price', this.value)" />
                </td>
            </tr>
            <tr>
                <td></td>
                <td><span id="show_price"></span>&nbsp;<span>VNĐ</span></td>
            </tr>
            <tr>
                <td class="right bold"><span>Hình thức : </span></td>
                <td>
                    <?php 
                    $i = 1;
                    foreach($arrayForm as $key=>$type) {
                        $checked = "";
                        if($rinfo[$data["rid"]]["forms"] == $key || $key == 1) {
                            $checked = "checked='checked'";
                        }
                        ?>
                        <input <?php print $checked?> name="forms" title="<?php print $arrayTitle[$i]?>" type="radio" onclick="setTextLocation(this)" value="<?php print $key?>"/><?php print $type?>
                        <?php 
                        $i++;
                    } 
                    ?>
                </td>
            </tr>
            <tr>
                <td class="right bold"><span id="lbl_location_index">Nơi bán : </span></td>
                <td>
                    <select name="location_index" class="location_index">
                        <option value="0">- Chọn nơi bán -</option>
                        <?php
                        foreach($arrayLocate as $location) { 
                            $selected = "";
                            if($rinfo[$data["rid"]]["location_index"] == $location["id"]) {
                                $selected = "selected='selected'";
                            }
                            ?>
                            <option <?php print $selected?> value="<?php print $location["id"]?>"><?php print $location["name"]?> (<span class="index"><?php print $location["index"]?></span>)</option>
                            <?php
                        }
                        ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td class="right bold"><span>Nơi đăng tin : </span></td>
                <td>
                    <select name="location_index_post" class="location_index_post">
                        <option value="0">- Chọn nơi đăng tin -</option>
                        <?php 
                        foreach($arrayLocate as $location) { 
                            $selected = "";
                            if($rinfo[$data["rid"]]["location_index_post"] == $location["id"]) {
                                $selected = "selected='selected'";
                            }
                            ?>
                            <option <?php print $selected?> value="<?php print $location["id"]?>"><?php print $location["name"]?> (<span class="index"><?php print $location["index"]?></span>)</option>
                            <?php 
                        } 
                        ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td class="right bold"><span>Thời gian đăng tin đến : </span></td>
                <td>
                    <input type="text" name="end_date" id="end_date" value="<?php print (isset($rinfo[$data["rid"]]["end_date"])) ? date("d/m/Y", $rinfo[$data["rid"]]["end_date"]) : ""?>" />
                </td>
            </tr>
            <tr>
                <td class="right bold">Tin hot : </td>
                <td>
                    <input type="checkbox" name="hot" value="1" <?php print ($rinfo[$data["rid"]]["hot"] == 1) ? "checked='checked'" : "" ?> />
                </td>
            </tr>
            <tr>
                <td class="right bold"><span>Mô tả ngắn : </span></td>
                <td>
                    <input type="text" name="short_description" class="short_description txt" value="<?php print $rinfo[$data["rid"]]["short_description"]?>" />
                </td>
            </tr>
            <tr>
                <td class="right bold"><span>Mô tả chi tiết : </span></td>
                <td>
                    <textarea name="description" class="description area"><?php print $rinfo[$data["rid"]]["description"]?></textarea>
                </td>
        </table>
    </div>
    <!-- End Form Info Raovat -->
    
    <!-- Technical info -->
    <div class="step2blocks">
        <?php
        if(count($data["att"]) > 0) {
        ?>
        <?php foreach ($data["att"] as $key => $value) { ?>
            <div class="h14">
                <?php echo $value["value"] ?>
            </div>
            <?php foreach ($value["key"] as $key1 => $value1) { ?>
                <div class="step2row">
                    <div class="left"><?php echo $value1["filter_name"] ?>&nbsp;:</div>
                    <div class="right">
                        <div class="listmang">
                            <ul>
                                <?php
                                $attribute = getAttributesValue($value1["filter_id"]);
                                if ($value1["filter_type"] == 0) {
                                    $array_value = unserialize($attribute[0][property_name]);
                                    //print_r($array_value);
                                    ?>
                                    <?php if ($array_value) { ?>
                                        <select name="combo_<?php echo $value1["filter_id"] ?>">
                                            <option value="">- Chọn thông số -</option>
                                            <?php
                                            if ($data["value"]) {//truong hop nay la sua thong so ki thuat,neu mang ton tai mang data["value"] thi tim gia tri da nhap de hien thi len
                                                $ok = 0;
                                                foreach ($array_value as $key2 => $value2) {
                                                    $check = 0; //nếu check = 0 thì giá giá trị này chưa có,check = 1 giá trị này đã có 
                                                    foreach ($data["value"] as $id => $val) {//với mỗi giá trị của trường trong bẳng category_filter_property thì sẽ duyệt mảng data["value"] xem có giá trị nào trùng ko?
                                                        if ($value1["filter_id"] == $val["filter_id"] && $val["property_name"] == $value2) {
                                                            $check = 1;
                                                        }
                                                    }//đã kiểm tra xong, có 2 trường hợp là check =0 or check = 1;
                                                    if ($check == 1) {
                                                        $ok = 1;
                                                        ?>
                                                        <option selected="selected">
                                                            <?php echo $value2 ?>
                                                        </option>
                                                    <?php } else { ?>
                                                        <option>
                                                            <?php echo $value2 ?>
                                                        </option>
                                                        <?php
                                                    }
                                                }
                                            } else {//truong hop la them moi
                                                foreach ($array_value as $key2 => $value2) {
                                                    ?>
                                                    <option>
                                                        <?php echo $value2 ?>
                                                    </option>
                                                <?php }
                                            } ?>
                                        </select>
                                        <span>Hoặc là nhập giá trị : </span><input type="text" name = text_<?php echo $value1["filter_id"] ?> value="<?php echo ($ok == 0)?$data["value"]["val_" . $value1["filter_id"]]:''?>" />
                                        <?php
                                    }
                                }
                                if ($value1["filter_type"] == 1) {
                                    $array_value = unserialize($attribute[0][property_name]);
                                    if ($array_value) {
                                        if (!$data["value"]) {//trường hợp là thêm mới
                                            foreach ($array_value as $key2 => $value2) {
                                                ?>
                                                <input type="checkbox" name="check_<?php echo $value1["filter_id"] ?>[]" value="<?php echo $value2; ?>" /><?php echo $value2; ?>
                                                <?php
                                            }
                                        } else {//trường hợp là sửa
                                            foreach ($array_value as $key2 => $value2) {
                                                $check = 0; //nếu check = 0 thì giá giá trị này chưa có,check = 1 giá trị này đã có 
                                                foreach ($data["value"] as $id => $val) {//với mỗi giá trị của trường trong bẳng category_filter_property thì sẽ duyệt mảng data["value"] xem có giá trị nào trùng ko?
                                                    if ($value1["filter_id"] == $val["filter_id"] && $val["property_name"] == $value2) {
                                                        $check = 1;
                                                    }
                                                }//đã kiểm tra xong, có 2 trường hợp là check =0 or check = 1;
                                                if ($check == 0) {
                                                    ?>
                                                    <input type="checkbox" name="check_<?php echo $value1["filter_id"] ?>[]" value="<?php echo $value2; ?>" /><?php echo $value2; ?>
                                                <?php } else { ?>
                                                    <input type="checkbox" checked="check" name="check_<?php echo $value1["filter_id"] ?>[]" value="<?php echo $value2; ?>" /><?php echo $value2; ?>
                                                    <?php
                                                }
                                            }
                                        }
                                    }
                                }
                                ?>
                            </ul>
                        </div>
                    </div>
                </div>
            <?php } ?>
        <?php } ?>
    </div>
    <?php
    }
    ?>
    <!-- End technical info -->
    
    <input id="submit-info" type="submit" name="submit" value="<?php print ($action == "post") ? "Đăng tin" : "Cập nhật"?>" />
</form>